package com.leyou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.mapper.BrandMapper;
import com.leyou.pojo.Brand;
import com.leyou.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<Brand> queryBrandByPageAndSort(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 开始分页
        PageHelper.startPage(page, rows);
        // 过滤
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().andLike("name", "%" + key + "%").orEqualTo("letter", key);
        }
        if (StringUtils.isNotBlank(sortBy)) {
            // 排序
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        // 查询
        List<Brand> brandList = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(brandList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        // 返回结果
        return new PageResult<>(pageInfo.getTotal(), brandList);
    }

    @Transactional
    public void addBrand(Brand brand, List<Long> cids) {
        // 新增品牌信息
        brandMapper.insertSelective(brand);
        if (brand.getId() != null) {
            for (Long cid : cids) {
                // 新增品牌和商品类目中间表数据
                brandMapper.insertCategoryBrand(cid, brand.getId());
            }
        }
    }

    @Transactional
    public void updBrand(Brand brand, List<Long> cids) {
        // 修改品牌信息
        brandMapper.updateByPrimaryKeySelective(brand);
        // 删除品牌和商品类目中间表数据
        brandMapper.deleteCategoryBrand(brand.getId());
        for (Long cid : cids) {
            // 新增品牌和商品类目中间表数据
            brandMapper.insertCategoryBrand(cid, brand.getId());
        }
    }

    @Transactional
    public void delBrand(Long bid) {
        // 删除品牌信息
        brandMapper.deleteByPrimaryKey(bid);
        // 删除品牌和商品类目中间表数据
        brandMapper.deleteCategoryBrand(bid);
    }

    @Override
    public List<Brand> queryBrandByCid(Long cid) {
        List<Brand> brands = brandMapper.queryBrandsByCid(cid);
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }

    /**
     * 通过id查询品牌
     *
     * @param id
     * @return
     */
    public Brand queryBrandById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过ids查询品牌
     *
     * @param ids
     * @return
     */
    public List<Brand> queryBrandByIds(List<Long> ids) {
        return brandMapper.selectByIdList(ids);
    }
}
